module(..., package.seeall)

local errnamemap={}

function RegErr(errid,errname,pattern)
	errnamemap[errname]=pattern
end

function GenErr(errname,...)
	local str=errname .. ':'
	for i=1,select("#",...) do
		if i>1 then
			str=str .. ','
		end
		str=str .. tostring(select(i,...))
	end
	return str
end

function GetErrStr(errmsg,lang)
	local pos=errmsg:find(':',1,true)
	local errname=errmsg:sub(1,pos and (pos-1))
	local pat=errnamemap[errname]
	if pat then
		local t=pat.type
		local lp=pat[lang] or pat[1]
		if not lp then return errmsg end
		pos=(pos or #errmsg)+1
		if t=='format' then
			local row = {}
			local cnt=1
			for value in errmsg:sub(pos):gmatch("[^`]+") do
				row[tostring(cnt)]=value
				cnt=cnt+1
			end
			return (lp:gsub('$(%d+)',row))
		else
			return lp
		end
	else
		return errmsg
	end
end
